package com.coolgor.axnut.interceptor.auth;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.HandlerInterceptor;

import com.coolgor.axnut.entity.auth.LocalUser;

/**
 * 系统登录验证拦截器
 * 
 * @author CoolGor
 *
 */
public class LoginInterceptor implements HandlerInterceptor {
	/**
	 * 主要做事前拦截，即用户操作发生前，改写preHandle里的逻辑，进行拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 从session中取出用户信息来
		Object localUserObj = request.getSession().getAttribute("login_user");
		if (localUserObj != null) {
			// 若用户信息不为空则将session里的用户信息转换成LocalUser实体类对象
			LocalUser localUser = (LocalUser) localUserObj;
			// 做空值判断，确保userId不为空并且该帐号的可用状态为1
			if (localUser != null 
//					&& localUser.getUserID() != null
					&& localUser.getUserID() > 0 
					&& localUser.getStatus() == 3) {
				// 若通过验证则返回true,拦截器返回true之后，用户接下来的操作得以正常执行
				return true;
			}
		}
		//设置编码
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		// 若不满足登录验证，则直接跳转到帐号登录页面
		PrintWriter out = response.getWriter();
		out.println("对不起，您的权限不足！<br>");
		out.println("请从权限系统或微信企业号登录！");
		return false;
	}
}
